<?php


namespace App\backend\services;


use App\backend\models\CategoryModel;
use App\backend\models\GoodsModel;
use App\Exceptions\InvalidRequestException;

class CategoryServices
{

    /**
     * 获取 - 商品分类列表
     *
     * @param $data //请求参数数组
     * @return array
     */
    public static function getList($data)
    {
        $where_data = [];
        if (isset($data['name']) && !empty($data['name'])) {
            $where_data[] = ['name','like',"%{$data['name']}%"];
        }

        $select_data = ['id', 'name', 'thumb', 'enabled', 'parent_id'];
        $list = CategoryModel::listed($where_data, [], $select_data);
        if (!empty($list)) {
            foreach ($list as $value) {
                $where_data = ['categorys' => $value->id];
                $value->goods_num = GoodsModel::goodsNumData($where_data);
            }
        }

        return CategoryModel::getTree($list);
    }

    /**
     * 添加/编辑 - 商品分类
     *
     * @param $data //请求参数数组
     * @return array
     */
    public static function categoryModify($data)
    {
        if (empty($data) && $data == false) {
            throw new InvalidRequestException(10009);
        }

        $id = isset($data['category_id']) && !empty($data['category_id']) ? $data['category_id'] : 0;unset($data['category_id']);

        if ($id) {
            $data['updated_at'] = time();
            $res = CategoryModel::updatedData($id, $data);
        } else {
            $data['created_at'] = time();
            $res = CategoryModel::insertData($data);
        }

        if ($res) {
            $select_data = ['id', 'name', 'thumb', 'enabled', 'parent_id'];
            $list = CategoryModel::listed([], [], $select_data);
            if (!empty($list)) {
                foreach ($list as $value) {
                    $where_data = ['categorys' => $value->id];
                    $value->goods_num = GoodsModel::goodsNumData($where_data);
                }
            }

            return CategoryModel::getTree($list);
        }

        return $res;
    }

    /**
     * 显示隐藏 - 商品分类
     *
     * @param $data //请求参数数组
     * @return array
     */
    public static function updateCategoryEnabled($data)
    {
        if (empty($data) && $data == false) {
            throw new InvalidRequestException(10009);
        }

        $update_data['enabled'] = !empty($data['enabled']) ? $data['enabled'] : 0;unset($data['enabled']);
        $update_data['deleted_at'] = time();

        $update_state = CategoryModel::deleteds($data, $update_data);
        if (empty($update_state)) {
            throw new InvalidRequestException(30006);
        }
        return [];
    }

    /**
     * 删除 - 商品分类
     *
     * @param $data //请求参数数组
     * @return array
     */
    public static function updateCategoryStatus($data)
    {
        if (empty($data) && $data == false) {
            throw new InvalidRequestException(10009);
        }

        $update_data['status'] = !empty($data['status']) ? $data['status'] : 0;unset($data['status']);
        $update_data['deleted_at'] = time();

        $update_state = CategoryModel::deleteds($data, $update_data);
        if (empty($update_state)) {
            throw new InvalidRequestException(30006);
        }
        return [];
    }

}
